<?php
namespace Company\Controller;
use Think\Controller;
class SalesmanController extends BaseController {
	public function _initialize()
    {
        parent::_initialize();
        $g=session('agent');
        C('erp_dbname',$g['db']);
    }
	public function add_cost(){
		$m=M('PushorderSalesmanCost', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$list_id=I('list_id');
		$ids=I('ids');
		$data['order_id']=$list_id;
		$count=0;
		$count_fail=0;
		foreach($ids as $k=>$v){
			$data['cost_id']=$v;
			$info=$m->where(array('order_id'=>$list_id,'cost_id'=>$v))->find();
			if(!$info){
				$res=$m->add($data);
				if(!$res){
					$return['status']=0;
					$return['message']="添加失败";
					$this->ajaxReturn($return);
				}else{
					$count++;
				}
			}else{
					$count_fail++;
			}
		}
		$return['status']=1;
		$return['message']=$count."个添加成功,".$count_fail."个添加失败";
		$this->ajaxReturn($return);
	}
	//修改
	public function cost_edit(){
		$m=M('PushorderSalesmanCost', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$prices=I('prices',trim);
		$ids=I('ids',trim);
		for($i=0;$i<count($prices);$i++){
			$data['total_fee']=$prices[$i];
			$id=$ids[$i];
			$res=$m->where(array('id'=>$id))->save($data);
			if($res!==false){
				continue;
			}else{
				$return['status']=0;
				$return['message']="更新失败！";
				$this->ajaxReturn($return);
			}
		}
		$return['status']=1;
		$return['message']="更新成功！";
		$this->ajaxReturn($return);
	}
	public function cost(){
		$Cost=M('Cost', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
        
		$keywords=I('get.keywords');     
		$selval=I('get.selval');     
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
        if(!empty($keywords)){
			if($selval=='1'){
				$map['no'] = array('like','%'.$keywords.'%');
			}
			if($selval=='2'){
				$map['name'] = array('like','%'.$keywords.'%');
			}
        }
		$count=$Cost->where($map)->count();
		$list=$Cost->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出

        $this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
        $this->display();            
    }
	//价格分摊
	public function price_share(){
		$m=M('PushorderSalesmanCost', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$country_model = M('country', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$id=I('id');
        $g=session('agent');
        $db_name=$g['db'];
        $f=C('DB_PREFIX_T');
        $this->p = M('PushordergoodsSalesman',$f, C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        $list=$this->p->where("order_id='".$id."'")->select();
		$list_cost=$m->alias('m')->field('m.*,c.no,c.name')->join('__COST__ c on m.cost_id=c.id')->where(array('order_id'=>$id))->select();
		
		$country = $country_model->select();
		$detail=M('PushorderSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'))->where('id='.$id)->find();
		
		$this->assign('country',$country);
		$this->assign('detail',$detail);
		
        $this->assign('order_no',$id);
        $this->assign('list',$list);
		$this->assign('list_cost',$list_cost);
        $this->display();
	}
	//按数量分摊
	public function share_num(){
		$id=I('id');
		$m=M('PushorderSalesmanCost', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$n=M('PushordergoodsSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$cost_list=$m->where(array('order_id'=>$id))->select();
		$total_fee=0;
		foreach($cost_list as $k=>$v){
			$total_fee+=$v['total_fee'];
		}
		$product_list=$n->where(array('order_id'=>$id))->select();
		$total_nums=0;
		foreach($product_list as $ks=>$vs){
			$total_nums+=$vs['goods_nums'];
		}
		foreach($product_list as $kss=>$vss){
			$data['share_fee']=$vss['goods_nums']/$total_nums*$total_fee;
			$res=$n->where(array('id'=>$vss['id']))->save($data);
			// echo $n->getlastsql();
			// die;
			if($res!==false){
				continue;
			}else{
				$return['status']=0;
				$return['message']="更新失败！";
				$this->ajaxReturn($return);
			}
		}
		$return['status']=1;
		$return['message']="更新成功！";
		$this->ajaxReturn($return);
	}
	//按金额分摊
	public function share_fee(){
		$id=I('id');
		$m=M('PushorderSalesmanCost', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$n=M('PushordergoodsSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$cost_list=$m->where(array('order_id'=>$id))->select();
		$total_fee=0;
		foreach($cost_list as $k=>$v){
			$total_fee+=$v['total_fee'];
		}
		$product_list=$n->where(array('order_id'=>$id))->select();
		$total_price=0;
		foreach($product_list as $ks=>$vs){
			$total_price+=$vs['goods_nums']*$vs['goods_price'];
		}
		foreach($product_list as $kss=>$vss){
			$data['share_fee']=$vss['goods_nums']*$vss['goods_price']/$total_price*$total_fee;
			$res=$n->where(array('id'=>$vss['id']))->save($data);
			// echo $n->getlastsql();
			// die;
			if($res!==false){
				continue;
			}else{
				$return['status']=0;
				$return['message']="更新失败！";
				$this->ajaxReturn($return);
			}
		}
		$return['status']=1;
		$return['message']="更新成功！";
		$this->ajaxReturn($return);
	}
	//推送价格
	public function push_price($id,$goods_price){
        if($id==''){
            alert(0,'操作有误');
        }
        $g=session('agent');
        $db_name=$g['db'];
        $f=C('DB_PREFIX_T');////因为对方现在有list_product表的id字段---gooods_id  所以不需要那么复杂
        $this->g = M('PushordergoodsSalesman',$f, C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
		$this->p = M('PushorderSalesman',$f, C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        $result=$this->g->where("id='".$id."'")->find();
		$list_info=$this->p->where("id='".$result['order_id']."'")->find();
        $data=array(
            'erp_code'=>$g['company_id'],
            'order_no'=>$list_info['order_no'],
			'goods_no'=>$result['goods_sn'],
            'goods_price'=>$goods_price,
        );
        ////根据参数找到对应的仓库
        $d=M("warehouse")->join("__INTERFACES__ on __INTERFACES__.warehouse_id=__WAREHOUSE__.id",'left')
            ->where(C('DB_PREFIX')."warehouse.warehouse_code='".$result['warehouse_code']."'")
            ->getField('vars,url,warehouse_code');
        $url=$d['var_8']['url'];

        $warehouse_code=$d['var_3']['warehouse_code'];
        $data['warehouse_code']=$warehouse_code;
        $return =push($url,$data);
        $result=json_decode($return,true);
		
        return $result;

    }
	//价格修改
	public function price_edit(){
		$m=M('PushordergoodsSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$prices=I('prices',trim);
		$ids=I('ids',trim);
		// dump(I());
		for($i=0;$i<count($prices);$i++){
			$data['goods_price']=$prices[$i];
			$id=$ids[$i];
			$res=$m->where(array('id'=>$id))->save($data);
			
			if($res!==false){
				$this->massage('销售单','价格修改','产品信息(ID='.$id.')价格修改为'.$prices[$i]);
				if($res==1){//推送数据
					$ss=$this->push_price($id,$prices[$i]);
					// dump($ss);
					// die;
					if($ss['status']){
						continue;
					}else{
						$return['status']=0;
						$return['message']=$ss['info'];
						$this->ajaxReturn($return);
					}
				}
				
			}else{
				$return['status']=0;
				$return['message']="更新失败！";
				$this->ajaxReturn($return);
			}
		}
		$return['status']=1;
		$return['message']="更新成功！";
		$this->ajaxReturn($return);
	}
	//确认损耗修改
	public function confirm_edit_wastage(){
		$m=M('PushordergoodsSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$nums=I('nums',trim);
		$prices=I('prices',trim);
		$ids=I('ids',trim);
		// dump(I());
		for($i=0;$i<count($prices);$i++){
			$data['wastage_goods_price']=$prices[$i];
			$data['wastage_goods_nums']=$nums[$i];
			$id=$ids[$i];
			$res=$m->where(array('id'=>$id))->save($data);
			
			if($res!==false){
				$this->massage('销售单','损耗修改','产品信息(ID='.$id.')损耗价格/数量修改为'.$prices[$i].'/'.$nums[$i]);
				if($res==1){//推送数据
					continue;
				}
				
			}else{
				$return['status']=0;
				$return['message']="更新失败！";
				$this->ajaxReturn($return);
			}
		}
		$return['status']=1;
		$return['message']="更新成功！";
		$this->ajaxReturn($return);
	}
	//销售单详情
    public function edit_wastage(){
			$param = I('param.','','trim');

			$id=$param['id'];
			
			$PushordergoodsSalesman = M('PushordergoodsSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$country_model = M('country', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$PushorderSalesman=M('PushorderSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$coin_model = M('coin', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
            $planPack_model = M('planPack', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
            $plan_pack = $planPack_model->select();
			$country = $country_model->select();
			
			$detail=M('PushorderSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'))->where('id='.$id)->find();
			$detail['wrap_type_name']=$planPack_model->where('code='.$detail['wrap_type'])->find();
			
			$listrows=10;

			$p=I('p')?I('p'):1;
			$firstrow=($p-1)*$listrows;

			$map['order_id']=$id;
			$count=$PushordergoodsSalesman->where($map)->count();
			$list   = $PushordergoodsSalesman->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
			
			$Page    = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show    = $Page->show();// 分页显示输出
			
			
		
			foreach($list as $k=>$v){
                $currency =$coin_model->where(array('coin_code'=>$v['currency']))->find();
                $list[$k]['currency_name'] = $currency['coin_name'];
            }
			
            $data['plan_pack']      = $plan_pack;
			$this->assign('page',$show);// 赋值分页输出
			$this->assign("list",$list);
			$this->assign("country",$country);
            $this->assign('detail',$detail);
			$this->assign('data',$data);
            $this->display();
    }
	//销售单详情
    public function list_edit_detail(){
			$param = I('param.','','trim');

			$id=$param['id'];
			
			$PushordergoodsSalesman = M('PushordergoodsSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$country_model = M('country', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$PushorderSalesman=M('PushorderSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$coin_model = M('coin', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
            $planPack_model = M('planPack', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
            $plan_pack = $planPack_model->select();
			$country = $country_model->select();
			
			$detail=M('PushorderSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'))->where('id='.$id)->find();
			$detail['wrap_type_name']=$planPack_model->where('code='.$detail['wrap_type'])->find();
			
			$listrows=10;

			$p=I('p')?I('p'):1;
			$firstrow=($p-1)*$listrows;

			$map['order_id']=$id;
			$count=$PushordergoodsSalesman->where($map)->count();
			$list   = $PushordergoodsSalesman->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
			
			$Page    = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show    = $Page->show();// 分页显示输出
			
			
		
			foreach($list as $k=>$v){
                $currency =$coin_model->where(array('coin_code'=>$v['currency']))->find();
                $list[$k]['currency_name'] = $currency['coin_name'];
            }
			
            $data['plan_pack']      = $plan_pack;
			$this->assign('page',$show);// 赋值分页输出
			$this->assign("list",$list);
			$this->assign("country",$country);
            $this->assign('detail',$detail);
			$this->assign('data',$data);
            $this->display();
    }
	//销售单修改
	public function list_edit(){
		$List=M('PushorderSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$keywords=I('get.keywords');     
		$selval=I('get.selval');     
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
        if(!empty($keywords)){
			if($selval=='1'){
				$map['order_no'] = array('like','%'.$keywords.'%');
			}
        }
		$map['push_status']=1;
		$count=$List->where($map)->count();
		$list=$List->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		foreach($list as $k=>$v){
			$list[$k]['section_name']=M('Section', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'))->where(array('no'=>$v['section_no']))->find();
			$list[$k]['people_make_name']=M('Personnel', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'))->where(array('no'=>$v['people_make_no']))->find();//制单人
			$list[$k]['people_purchasing_name']=M('Personnel', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'))->where(array('no'=>$v['people_purchasing_no']))->find();//采购申请人
			// $list[$k]['auditor_name']=M('User')->where(array('username'=>$v['auditor']))->find();//审核人
			// echo M('User')->getlastsql();
		}
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出

        $this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
        $this->display();            
    }
    //用户列表
    public function index(){
		//仓库列表
		$g=session('agent');
        $user_id=$g['company_id'];
        $group_id=M("user_warehouse")->where(array('user_id'=>$user_id))->getField('warehouse_group_id');
        $group_id=explode(',',$group_id);
        $where['id']=array('in',$group_id);
        $warehouse_list=M("warehouse")->field('id,name')->where($where)->select();
		// dump($warehouse_list);
		$PushorderSalesman=M('PushorderSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
        $keywords=I('get.keywords');     
		$selval=I('get.selval');     
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
		
		$map['section_no']=array('neq','');
        if(!empty($keywords)){
			if($selval=='1'){
				$map['order_no'] = array('like','%'.$keywords.'%');
			}
			if($selval=='2'){
				$map['auditor'] = array('like','%'.$keywords.'%');
			}
        }
		$count=$PushorderSalesman->where($map)->count();
		$list=$PushorderSalesman->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		foreach($list as $k=>$v){
			$list[$k]['section_name']=M('Section', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'))->where(array('no'=>$v['section_no']))->find();
			$list[$k]['people_make_name']=M('Personnel', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'))->where(array('no'=>$v['people_make_no']))->find();//制单人
			$list[$k]['people_purchasing_name']=M('Personnel', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'))->where(array('no'=>$v['people_purchasing_no']))->find();//采购申请人
			// $list[$k]['auditor_name']=M('User')->where(array('username'=>$v['auditor']))->find();//审核人
			// echo M('User')->getlastsql();
		}
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		
		
		
        $this->assign("count",$count);
		$this->assign("warehouse_list",$warehouse_list);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
        $this->display();            
    }

    //新增
    public function add(){
		$this->tslist();
        if(IS_POST){
			$PushorderSalesman=M('PushorderSalesman',C('DB_PREFIX_T'),C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$order_no=trim(I('order_no'));
			$status=$PushorderSalesman->where(array('order_no'=>$order_no))->find();
			if($status){
				$this->error('单号重复');
			}
			$data['order_no']=$order_no;

			$data['section_no']=trim(I('section_no'));
			$data['people_make_no']=trim(I('people_make_no'));
			$data['people_purchasing_no']=trim(I('people_purchasing_no'));
			$data['contacts_company_no']=trim(I('contacts_company_no'));
			$data['user_name']=trim(I('user_name'));
			$data['user_address']=trim(I('user_address'));
			$data['transport_cate']=trim(I('transport_cate'));
			if(!mobile(trim(I('user_phone')))){
				$this->error('联系电话(手机号)错误');
			}
			$data['user_phone']=trim(I('user_phone'));
			$data['user_idcard']=trim(I('user_idcard'));
			$data['province']=trim(I('province'));
			$data['city']=trim(I('city'));
			$data['area']=trim(I('area'));
			$data['make_add_time']=time();//制单时间
			if(I('transport_cate')==1){
				$data['delivery_code']=rand(1000,9999).time();
			}
			if(I('transport_cate')==2){
				$data['logistics_no']=trim(I('logistics_no'));
			}
			$res=$PushorderSalesman->add($data);
			// echo $PushorderSalesman->getlastsql();
            if($res){
				$this->success('添加成功');
			}
        }else{
			$this->display();
		}
    }
     /*
     * 编辑入库单、添加商品
     */
    public function edt(){
		$this->tslist();
        $param = I('param.','','trim');
		$PushorderSalesman = M('PushorderSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$g=M('PushordergoodsSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$ao=M('ArrivalOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
        if(IS_POST){
            $order_no = $param['order_no'];
			$id=$param['id'];
			$map['order_no']=$order_no;
			$map['id']=array('neq',$id);
			$maps['order_no']=$order_no;
            $plan_no =  $PushorderSalesman->where($map)->find();
            if($plan_no){
                $this->error('单号重复');
            }
            //$param['add_time'] = time();
			unset($param['strss']);
            $ress = $PushorderSalesman->where('id='.$id)->save($param);
            if($ress!==false){
				$str=explode(',',I('strss'));
				foreach($str as $k=>$v){
					$arr=explode(':',$v);
					$where['id']=$arr[0];
					$data['goods_price']=$arr[1];
					//$data['total_price']=$arr[1]*$arr[2];
					$data['goods_nums']=$arr[2];
					$data['source']=$arr[3];
					$where['order_id']=$param['id'];
					$goods_info=$g->where($where)->find();
					$ao_info=$ao->where(array('id'=>$goods_info['goods_id']))->find();
					if($ao_info['storage_amount']<$arr[2]){
						$this->error('数量大于库存数量');
						die;
					}
					//扣库存-begin
					$plan_nos = $PushorderSalesman->where($maps)->find();
					if($plan_nos['add_goods']==1){//已添加，则修改数据
						if($goods_info['goods_nums']>$arr[2]){
							$goods_nums=$goods_info['goods_nums']-$arr[2];
							$ao->where(array('id'=>$goods_info['goods_id']))->save(array('freez_amount'=>array('exp','freez_amount-'.$goods_nums),'storage_amount'=>array('exp','storage_amount+'.$goods_nums)));//冻结库存
						}else{
							$goods_nums=$arr[2]-$goods_info['goods_nums'];
							$ao->where(array('id'=>$goods_info['goods_id']))->save(array('freez_amount'=>array('exp','freez_amount+'.$goods_nums),'storage_amount'=>array('exp','storage_amount-'.$goods_nums)));//冻结库存
						}
					}else{
						$ao->where(array('id'=>$goods_info['goods_id']))->save(array('freez_amount'=>array('exp','freez_amount+'.$arr[2]),'storage_amount'=>array('exp','storage_amount-'.$arr[2])));//冻结库存
					}
					//扣库存-end
					$status=$g->where($where)->save($data);
					if($status!==false){
						$res=$PushorderSalesman->where('id='.$id)->setField('add_goods',1);
						if($res!==false){
							continue;
						}else{
							$this->error('更新添加商品状态出错');
						}
					}else{
						$this->error('修改失败');
					}
				}
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }else{
			$id=$param['id'];
			
			$PushordergoodsSalesman = M('PushordergoodsSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$country_model = M('country', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$PushorderSalesman=M('PushorderSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$coin_model = M('coin', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
            $planPack_model = M('planPack', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
            $plan_pack = $planPack_model->select();
			$country = $country_model->select();
			
			$detail=$PushorderSalesman->where('id='.$id)->find();
			$detail['wrap_type_name']=$planPack_model->where('code='.$detail['wrap_type'])->find();
			
			$listrows=10;

			$p=I('p')?I('p'):1;
			$firstrow=($p-1)*$listrows;

			$map['order_id']=$id;
			$count=$PushordergoodsSalesman->where($map)->count();
			$list   = $PushordergoodsSalesman->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
			
			$Page    = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show    = $Page->show();// 分页显示输出

			foreach($list as $k=>$v){
                $currency =$coin_model->where(array('coin_code'=>$v['currency']))->find();
                $list[$k]['currency_name'] = $currency['coin_name'];
            }
			
            $data['plan_pack']      = $plan_pack;
			$this->assign('page',$show);// 赋值分页输出
			$this->assign("list",$list);
			$this->assign("country",$country);
            $this->assign('detail',$detail);
			$this->assign('data',$data);
            $this->display();
        }
    }
	//成品单审批
	public function examined(){
		// $warehouse_code=session('warehouse_code');
		$keywords=I('get.keywords');     
		$selval=I('get.selval');     
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
		$PushorderSalesman=M('PushorderSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$map['add_goods']=1;//为1时确定已经添加过商品
		// $map['warehouse_code']=$warehouse_code;
        if(!empty($keywords)){
			if($selval=='1'){
				$map['order_no'] = array('like','%'.$keywords.'%');
			}
			if($selval=='2'){
				$map['auditor'] = array('like','%'.$keywords.'%');
			}
        }
		$count=$PushorderSalesman->where($map)->count();
		$list=$PushorderSalesman->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		foreach($list as $k=>$v){
			$list[$k]['section_name']=M('Section', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'))->where(array('no'=>$v['section_no']))->find();
			$list[$k]['people_make_name']=M('Personnel', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'))->where(array('no'=>$v['people_make_no']))->find();//制单人
			$list[$k]['people_purchasing_name']=M('Personnel', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'))->where(array('no'=>$v['people_purchasing_no']))->find();//采购申请人
			// $list[$k]['auditor_name']=M('User')->where(array('username'=>$v['auditor']))->find();//审核人
		}
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
				
        $this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
        $this->display();            
		
	}
		//成品单二次审批
	public function examined_two(){
		$warehouse_code=session('warehouse_code');
		$keywords=I('get.keywords');     
		$selval=I('get.selval');     
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
		$PushorderSalesman=M('PushorderSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$map['add_goods']=1;//为1时确定已经添加过商品
		$map['warehouse_code']=$warehouse_code;
		$map['examined_status']=1;
        if(!empty($keywords)){
			if($selval=='1'){
				$map['order_no'] = array('like','%'.$keywords.'%');
			}
			if($selval=='2'){
				$map['auditor'] = array('like','%'.$keywords.'%');
			}
        }
		$count=$PushorderSalesman->where($map)->count();
		$list=$PushorderSalesman->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		foreach($list as $k=>$v){
			$list[$k]['section_name']=M('Section', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'))->where(array('no'=>$v['section_no']))->find();
			$list[$k]['people_make_name']=M('Personnel', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'))->where(array('no'=>$v['people_make_no']))->find();//制单人
			$list[$k]['people_purchasing_name']=M('Personnel', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'))->where(array('no'=>$v['people_purchasing_no']))->find();//采购申请人
			// $list[$k]['auditor_name']=M('User', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'))->where(array('username'=>$v['auditor']))->find();//审核人
		}
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
				
        $this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
        $this->display();            
		
	}
		//成品单三次审批
	public function examined_three(){
		$warehouse_code=session('warehouse_code');
		$keywords=I('get.keywords');     
		$selval=I('get.selval');     
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
		$PushorderSalesman=M('PushorderSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$map['add_goods']=1;//为1时确定已经添加过商品
		$map['examined_two_status']=1;
		$map['warehouse_code']=$warehouse_code;
        if(!empty($keywords)){
			if($selval=='1'){
				$map['order_no'] = array('like','%'.$keywords.'%');
			}
			if($selval=='2'){
				$map['auditor'] = array('like','%'.$keywords.'%');
			}
        }
		$count=$PushorderSalesman->where($map)->count();
		$list=$PushorderSalesman->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		foreach($list as $k=>$v){
			$list[$k]['section_name']=M('Section', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'))->where(array('no'=>$v['section_no']))->find();
			$list[$k]['people_make_name']=M('Personnel', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'))->where(array('no'=>$v['people_make_no']))->find();//制单人
			$list[$k]['people_purchasing_name']=M('Personnel', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'))->where(array('no'=>$v['people_purchasing_no']))->find();//采购申请人
			// $list[$k]['auditor_name']=M('User', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'))->where(array('username'=>$v['auditor']))->find();//审核人
		}
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
				
        $this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
        $this->display();            
		
	}
	/*
    * 审核
    */
	public function examinedset(){
		$data['examined_status']=I('examined_status');
		$data['auditor']=I('auditor');
		$data['examined_time']=time();
		$id=I('id');
		// dump($data);
		// die;
		if(I('examined_status')==0){
			$this->error('请选择审核状态');
			exit;
		}
		$m=M('PushorderSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$n=M('PushordergoodsSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$info=$n->where(array('order_id'=>$id))->select();
		$order_info=$m->where(array('id'=>$id))->find();
		if($order_info['examined_status']==1){
			$this->error('已审核通过不能更改');
		}
		if(empty($info)){
			$this->error('没有商品信息，不能审核！');
		}
		$res=$m->where("id=$id")->save($data);
		// dump(M());
		// die;
		if($res!==false){
			$this->success('审核状态更改成功');
		}
	}
	/*
    * 二次审核
    */
	public function examinedset_two(){
		$data['examined_two_status']=I('examined_two_status');
		$data['auditor_two']=I('auditor_two');
		$data['examined_two_time']=time();
		$id=I('id');
		if(I('examined_two_status')==0){
			$this->error('请选择审核状态');
			exit;
		}
		$res=M('PushorderSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'))->where("id=$id")->save($data);
		if($res!==false){
			$this->success('审核状态更改成功');
		}
	}
	 /*
     * 三次审核
     */
	public function examinedset_three(){
		$data['examined_three_status']=I('examined_three_status');
		$data['auditor_three']=I('auditor_three');
		$data['examined_three_time']=time();
		$id=I('id');
		if(I('examined_three_status')==0){
			$this->error('请选择审核状态');
			exit;
		}
		$res=M('PushorderSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'))->where("id=$id")->save($data);
		if($res!==false){
			$this->success('审核状态更改成功');
		}
	}
	/*
     * 打印
     */
    public function print_examinedetail(){
			$param = I('param.','','trim');
			$id=$param['id'];
			
			$PushordergoodsSalesman = M('PushordergoodsSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$country_model = M('country', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$PushorderSalesman=M('PushorderSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$coin_model = M('coin', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$Section=M('Section', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$ContactsCompany=M('ContactsCompany', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$Personnel=M('Personnel', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$unit = M('Unit', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			
			$warehouse=M('Warehouse');
			
			$country = $country_model->select();
			
			$detail = $PushorderSalesman->alias('m')->join('__CONTACTS_COMPANY__ c on m.contacts_company_no=c.no','left')->field('m.*,c.user_name,c.user_phone,c.user_idcard,c.province,c.city,c.area,c.user_address')->where('m.id='.$id)->find();
			
			
			$detail['section_name']=$Section->where(array('no'=>$detail['section_no']))->find();
			$detail['company_name']=$ContactsCompany->where(array('no'=>$detail['contacts_company_no']))->find();//往来单位
			$detail['people_make_name']=$Personnel->where(array('no'=>$detail['people_make_no']))->find();//制单人
			$detail['people_purchasing_name']=$Personnel->where(array('no'=>$detail['people_purchasing_no']))->find();
			
			
			$listrows=10;

			$p=I('p')?I('p'):1;
			$firstrow=($p-1)*$listrows;

			$map['order_id']=$id;
			$count=$PushordergoodsSalesman->where($map)->count();
			$list   = $PushordergoodsSalesman->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
			// dump($list);
			// die;
			$Page    = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show    = $Page->show();// 分页显示输出
			
			
			$count_goods_nums=0;//主数量
			$count_goods_price=0;//销售总金额
			$count_second_count=0;//辅数量
			foreach($list as $k=>$v){
                $currency =$coin_model->where(array('coin_code'=>$v['currency']))->find();
                $list[$k]['currency_name'] = $currency['coin_name'];
				$list[$k]['warehouse_info']=$warehouse->where(array('warehouse_code'=>$v['warehouse_code']))->find();
				$list[$k]['unit1_name']=$unit->where(array('unit_no'=>$v['first_unit']))->find();
				$list[$k]['unit2_name']=$unit->where(array('unit_no'=>$v['second_unit']))->find();
				$count_goods_nums+=$v['goods_nums'];
				$count_goods_price+=$v['goods_nums']*$v['goods_price'];
				$count_second_count+=$v['second_count'];
            }
			
			$this->assign('count_goods_nums',$count_goods_nums);
			$this->assign('count_goods_price',$count_goods_price);
			$this->assign('count_second_count',$count_second_count);
			
			$this->assign('page',$show);// 赋值分页输出
			$this->assign("list",$list);
			$this->assign("country",$country);
            $this->assign('detail',$detail);
			$this->assign('data',$data);
            $this->display();

     

    }
	 /*
     * 编辑入库单、添加商品
     */
    public function examinedetail(){
			$this->tslist();
			$param = I('param.','','trim');

			$id=$param['id'];
			
			$PushordergoodsSalesman = M('PushordergoodsSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$country_model = M('country', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$PushorderSalesman=M('PushorderSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$coin_model = M('coin', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
            $planPack_model = M('planPack', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
            $plan_pack = $planPack_model->select();
			$country = $country_model->select();
			
			$detail=M('PushorderSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'))->where('id='.$id)->find();
			$detail['wrap_type_name']=$planPack_model->where('code='.$detail['wrap_type'])->find();
			
			$listrows=10;

			$p=I('p')?I('p'):1;
			$firstrow=($p-1)*$listrows;

			$map['order_id']=$id;
			$count=$PushordergoodsSalesman->where($map)->count();
			$list   = $PushordergoodsSalesman->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
			
			$Page    = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show    = $Page->show();// 分页显示输出
			
			
		
			foreach($list as $k=>$v){
                $currency =$coin_model->where(array('coin_code'=>$v['currency']))->find();
                $list[$k]['currency_name'] = $currency['coin_name'];
            }
			
            $data['plan_pack']      = $plan_pack;
			$this->assign('page',$show);// 赋值分页输出
			$this->assign("list",$list);
			$this->assign("country",$country);
            $this->assign('detail',$detail);
			$this->assign('data',$data);
            $this->display();

     

    }
	 /*
     * 编辑入库单、添加商品
     */
    public function examinedetail_two(){
			$this->tslist();
			$param = I('param.','','trim');

			$id=$param['id'];
			
			$PushordergoodsSalesman = M('PushordergoodsSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$country_model = M('country', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$PushorderSalesman=M('PushorderSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$coin_model = M('coin', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
            $planPack_model = M('planPack', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
            $plan_pack = $planPack_model->select();
			$country = $country_model->select();
			
			$detail=M('PushorderSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'))->where('id='.$id)->find();
			$detail['wrap_type_name']=$planPack_model->where('code='.$detail['wrap_type'])->find();
			
			$listrows=10;

			$p=I('p')?I('p'):1;
			$firstrow=($p-1)*$listrows;

			$map['order_id']=$id;
			$count=$PushordergoodsSalesman->where($map)->count();
			$list   = $PushordergoodsSalesman->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
			
			$Page    = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show    = $Page->show();// 分页显示输出
			
			
		
			foreach($list as $k=>$v){
                $currency =$coin_model->where(array('coin_code'=>$v['currency']))->find();
                $list[$k]['currency_name'] = $currency['coin_name'];
            }
			
            $data['plan_pack']      = $plan_pack;
			$this->assign('page',$show);// 赋值分页输出
			$this->assign("list",$list);
			$this->assign("country",$country);
            $this->assign('detail',$detail);
			$this->assign('data',$data);
            $this->display();

     

    }
	 /*
     * 编辑入库单、添加商品
     */
    public function examinedetail_three(){
			$this->tslist();
			$param = I('param.','','trim');

			$id=$param['id'];
			
			$PushordergoodsSalesman = M('PushordergoodsSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$country_model = M('country', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$PushorderSalesman=M('PushorderSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$coin_model = M('coin', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
            $planPack_model = M('planPack', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
            $plan_pack = $planPack_model->select();
			$country = $country_model->select();
			
			$detail=M('PushorderSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'))->where('id='.$id)->find();
			$detail['wrap_type_name']=$planPack_model->where('code='.$detail['wrap_type'])->find();
			
			$listrows=10;

			$p=I('p')?I('p'):1;
			$firstrow=($p-1)*$listrows;

			$map['order_id']=$id;
			$count=$PushordergoodsSalesman->where($map)->count();
			$list   = $PushordergoodsSalesman->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
			
			$Page    = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show    = $Page->show();// 分页显示输出
			
			
		
			foreach($list as $k=>$v){
                $currency =$coin_model->where(array('coin_code'=>$v['currency']))->find();
                $list[$k]['currency_name'] = $currency['coin_name'];
            }
			
            $data['plan_pack']      = $plan_pack;
			$this->assign('page',$show);// 赋值分页输出
			$this->assign("list",$list);
			$this->assign("country",$country);
            $this->assign('detail',$detail);
			$this->assign('data',$data);
            $this->display();

     

    }
	/*
     * 商品列表
     */
    public function goods(){
			// $warehouse_code=session('warehouse_code');
			$Goods = M('ArrivalOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$listrows=10;

			$keyword=I('keyword')?I('keyword'):0;
			$plan_id=I('plan_id')?I('plan_id'):0;
			$id=I('id');
			$now_plan_id=$plan_id?$plan_id:$id;
			$p=I('p')?I('p'):1;
			$firstrow=($p-1)*$listrows;
			// $map['warehouse_code']=$warehouse_code;
			$map['status']=1;//已上架
			$map['storage_amount']=array('GT',0);//库存大于0
			if($keyword){
				$map['goods_no']=array('like',"%$keyword%");
			}
			$count=$Goods->where($map)->count();
			$list   = $Goods->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
			// dump($Goods);
			// die;
			$coin_model = M('coin', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
            $unit_model =  M('unit', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
            foreach($list as $k=>$v){
                $current_type = $coin_model->where(array('coin_code'=>$v['currency']))->find();
                $unit = $unit_model->where(array('unit_no'=>$v['declare_unit']))->find();

                if($v['unit1']){
                    $unit1 = $unit_model->where(array('unit_no'=>$v['unit1']))->find();
                }

                if($v['unit2']){
                    $unit2 = $unit_model->where(array('unit_no'=>$v['unit2']))->find();
                }

                $list[$k]['coin_name']   =  $current_type['coin_name'];
                $list[$k]['unit_name']   =  $unit['unit_name'];
                $list[$k]['unit1_name']  =  $unit1['unit_name'];
                $list[$k]['unit2_name']  =  $unit2['unit_name'];
            }
			$Page    = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show    = $Page->show();// 分页显示输出
			
			$this->assign('page',$show);// 赋值分页输出
			$this->assign("list",$list);
            $this->assign("plan_id",$now_plan_id);
            $this->display();
    }
	/*
     * 添加商品
     */
    public function add_goods(){

		$param = I('param.','','trim');
		if(!$param['ids']){
            $this->error("请选择商品");
        }
        $goods_ids = $param['ids'];
		
        $plan_id = $param['plan_id'];
		
		$product = M('Product', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		
        $PushorderSalesman = M('PushorderSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
        $PushordergoodsSalesman = M('PushordergoodsSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		
        // $plan_no = $PushorderSalesman->where($map)->find($plan_id);
		$error=0;
		
        foreach($goods_ids as $k=>$v){
			$map['id']=$v;
            $goods = M('ArrivalOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'))->where($map)->find($v);
			// dump($goods);
			// die;
			
            $plan_goods = array();
            $plan_goods['order_id'] = $plan_id;
			
            $plan_goods['goods_sn'] = $goods['goods_no'];
			$plan_goods['goods_name'] = $goods['goods_name'];
			$plan_goods['goods_price'] = $goods['price'];//产品单价
			$plan_goods['spec'] = $goods['goods_spec'];
			
			$product_info=$product->where(array('goods_no'=>$goods['goods_no']))->find();
			$plan_goods['source'] = $product_info['country_code'];
            $plan_goods['goods_unit'] = $goods['declare_unit'];
			$plan_goods['first_unit'] = $goods['unit1'];
			$plan_goods['second_unit'] = $goods['unit2'];
			$plan_goods['bar_code'] = $goods['bar_code'];
			$plan_goods['goods_batch_no'] = $goods['batch_no'];
			$plan_goods['goods_id'] = $goods['id'];
			$plan_goods['warehouse_code'] = $goods['warehouse_code'];
            $plan_goods['addtime'] = time();

            $where = array();
            $where['order_id'] = $plan_id;
			
			$where['goods_id'] = $v;
			
            $exit = $PushordergoodsSalesman->where($where)->find();

            if($exit){
                $error++;
            }else{
                $status=$PushordergoodsSalesman->add($plan_goods);
				// dump($PushordergoodsSalesman);
				// dump(333);
// die;
            };
        }

        if($error){
            $mes = "有".$error.'条数据添加失败 其他的添加成功';
            $this->error($mes);
        }else{
            $this->success("成功");
        }
	}



    //删除
    public function del(){
        $id=I('post.id');
        $res=M("Sampling", C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'))->where("id='$id'")->delete();
        if($res!==false){
            echo "y";
        }else{
            echo "操作失败";
        }
    }


	/**
	*详细商品列表
	**/
	public function goodsdetail(){
		$id=I('id')?I('id'):0;
		$goods_no=trim(I('goods_no'));
		$view=I('view');
		if($id){
			$Goods=M('SamplingDetail', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$detail=$Goods->where("plan_id=$id and goods_no='$goods_no'")->find();

			$detail['goods_name']=M('PurchasingGoods', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'))->where("goods_no='".$detail['goods_no']."'")->find();
			$this->assign("detail",$detail);
		}else{
			$this->error('请关联商品id');
		}
		if($view==1){
			$this->display('goodsdetailview');
		}else{
			$this->display();
		}
		
	}

	
	//生产计划单常用列表
	public function tslist(){
		$Section=M('Section', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$Personnel=M('Personnel', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$ContactsCompany=M('ContactsCompany', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$PushorderSalesman=M('PushorderSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$section_list=$Section->select();
		$personnel_list=$Personnel->select();
		$company_list=$ContactsCompany->select();
		//当日流水号生成
		$map['_string']="make_add_time-UNIX_TIMESTAMP(DATE(NOW()))>0 and make_add_time-UNIX_TIMESTAMP(DATE(NOW()))<86400";
		$count=$PushorderSalesman->where($map)->count();//当天添加的采购单数
		//echo $PushorderSalesman->getlastsql();
		$count=$count+1;
		$serial_number="";
		if($count<10){
			$serial_number='0000'.strval($count);
		}
		if($count>=10 and $count<100){
			$serial_number='000'.$count;
		}
		if($count>=100 and $count<1000){
			$serial_number='00'.$count;
		}
		if($count>=1000 and $count<10000){
			$serial_number='0'.$count;
		}
		$this->assign('serial_number',$serial_number);//流水序号
		$this->assign('section_list',$section_list);
		$this->assign('personnel_list',$personnel_list);
		$this->assign('company_list',$company_list);
	}
	//销售单接口
	public function pushorder(){
        $ids = I('param.ids','','trim');
        $m = M('PushorderSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
        $n = M('PushordergoodsSalesman', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$contactsCompany = M('ContactsCompany', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$map['m.id']=array('in',$ids);
		$maps['id']=array('in',$ids);
		
		$warehouse_list=$n->field('warehouse_code')->where(array('order_id'=>array('in',$ids)))->group('warehouse_code')->select();//取出仓库编码
		$count_warehouse=count($warehouse_list);//仓库个数
		// dump($warehouse_list);
		// die;
		$data = array();
		if($count_warehouse==1){//就一个仓库编码
			$list=$m->alias('m')->where($map)->select();
			// $list = $m->alias('m')->join('__CONTACTS_COMPANY__ c on m.contacts_company_no=c.no','left')->field('m.id,m.order_no,m.examined_status,c.user_name,c.user_phone,c.user_idcard,c.province,c.city,c.area,c.user_address,m.logistics_no,m.logistics_business,m.transport_cate,m.delivery_code')->where($map)->select();
			foreach($list as $k=>$v){
				if($v['examined_status']!=1){
					$return['status']=0;
					$return['info']='未审核通过不能推送';
					$this->ajaxReturn($return);
					die;
				}
				if(!empty($v['contacts_company_no'])){//不为0,关联往来单位
					$list_contacts=$contactsCompany->where(array('no'=>$v['contacts_company_no']))->find();
					$list[$k]['user_name']=$list_contacts['user_name'];
					$list[$k]['province']=$list_contacts['province'];
					$list[$k]['city']=$list_contacts['city'];
					$list[$k]['area']=$list_contacts['area'];
					$list[$k]['user_address']=$list_contacts['user_address'];
					$list[$k]['user_idcard']=$list_contacts['user_idcard'];
					$list[$k]['user_phone']=$list_contacts['user_phone'];
				}
				$list[$k]['platform_name']='ERP';
				$list[$k]['order_cate']=2;
				$list[$k]['company_name']=session('agent.company');
				$list[$k]['order_detail']=$n->field('goods_name,goods_sn,spec,goods_nums,goods_unit,bar_code,goods_batch_no')->where(array('order_id'=>$v['id']))->select();
			}
			$count=count($list);//总条数
			//接口地址
			$warehouse_info=M("warehouse")->where(array('warehouse_code'=>$warehouse_list[0]['warehouse_code']))->getField('id');
			// dump(M("warehouse")->getlastsql());
			// die;
			$d=M("warehouse")->join("__INTERFACES__ on __INTERFACES__.warehouse_id=__WAREHOUSE__.id")->where(C('DB_PREFIX')."warehouse.id='".$warehouse_info."'")->getField('vars,url,warehouse_code');
			
			$warehouse_code=$d['var_6']['warehouse_code'];
			$data['order_total']=$count;
			$data['info']=$list;
			$data['warehouse_code']=$warehouse_code;
			$url=$d['var_6']['url'];

			$res = send_curl($url,$data);
			
			$json=json_decode($res,true);
			if($json['status']){
				$m->where($maps)->setField('push_status',1);
			}
			$this->ajaxReturn($json);
			
		}else if($count_warehouse>1){//两个或两个以上仓库
			
			foreach($warehouse_list as $ks=>$vs){
				$list=$m->alias('m')->where($map)->select();
				// $list = $m->alias('m')->join('__CONTACTS_COMPANY__ c on m.contacts_company_no=c.no','left')->field('m.id,m.order_no,m.examined_status,c.user_name,c.user_phone,c.user_idcard,c.province,c.city,c.area,c.user_address,m.logistics_no,m.logistics_business,m.transport_cate,m.delivery_code')->where($map)->select();
				foreach($list as $k=>$v){
					if($v['examined_status']!=1){
						$return['status']=0;
						$return['info']='未审核通过不能推送';
						$this->ajaxReturn($return);
						die;
					}
					if(!empty($v['contacts_company_no'])){//不为0,关联往来单位
						$list_contacts=$contactsCompany->where(array('no'=>$v['contacts_company_no']))->find();
						$list[$k]['user_name']=$list_contacts['user_name'];
						$list[$k]['province']=$list_contacts['province'];
						$list[$k]['city']=$list_contacts['city'];
						$list[$k]['area']=$list_contacts['area'];
						$list[$k]['user_address']=$list_contacts['user_address'];
						$list[$k]['user_idcard']=$list_contacts['user_idcard'];
						$list[$k]['user_phone']=$list_contacts['user_phone'];
					}
					$list[$k]['platform_name']='ERP';
					$list[$k]['order_cate']=2;
					$list[$k]['company_name']=session('agent.company');
					$list[$k]['order_detail']=$n->field('goods_name,goods_sn,spec,goods_nums,goods_unit,bar_code,goods_batch_no')->where(array('order_id'=>$v['id'],'warehouse_code'=>$vs['warehouse_code']))->select();
					if(!count($list[$k]['order_detail'])){
						unset($list[$k]);
					}
				}
				$count=count($list);//总条数
				//接口地址
				$warehouse_info=M("warehouse")->where(array('warehouse_code'=>$vs['warehouse_code']))->getField('id');
				
				$d=M("warehouse")->join("__INTERFACES__ on __INTERFACES__.warehouse_id=__WAREHOUSE__.id")->where(C('DB_PREFIX')."warehouse.id='".$warehouse_info."'")->getField('vars,url,warehouse_code');
				$warehouse_code=$d['var_6']['warehouse_code'];
				$data['order_total']=$count;
				$data['info']=$list;
				$data['warehouse_code']=$warehouse_code;
				$url=$d['var_6']['url'];
				
				// $url = "http://es.jm.com/index.php?s=/Pushreceiveapi/pushorder";//wms订单接口
				$res = send_curl($url,$data);
				
				$json=json_decode($res,true);
				if($json['status']){
					$m->where($maps)->setField('push_status',1);
					continue;
				}else{
					$this->ajaxReturn($json);
				}
			}
			$return['status']=true;
			$return['info']="推送成功";
			$this->ajaxReturn($json);
		}else{//没有仓库，报错
			$return['status']=false;
			$return['info']="没有仓库数据，请查看列表内容";
			$this->ajaxReturn($return);
		}
    }
    public function massage($type,$title,$detail){
        $massage['url']='';
        $massage['type']=$type;
        $massage['title']=$title;
        $massage['detail']=$detail;
		M('massage', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'))->add($massage);
    }
}